/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2016-01-13 
 ******************************************************************************/
package cn.cmcc.online.smsapi.sdk.db.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;

import android.content.ContentValues;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.DBManager;
import cn.cmcc.online.smsapi.sdk.db.XyCursor;
import cn.cmcc.online.smsapi.sdk.db.base.BaseManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.util.Signaturer;
import cn.cmcc.online.smsapi.sdk.util.DateUtils;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;

public class menuActionManager {

    public static final String PHONE_NUM = "phone_num";// 短信号码

    public static final String DATE = "date";// 日期

    public static final String CLICK_TIMES = "click_times";// 点击次数

    public static final String COMPANY_NUM = "company_num";// 企业号
    
    public static final String FUNCTION_MODE = "function_mode";// 功能模块

    public static final String TABLE_NAME = "tb_menu_action";

    public static final String DROP_TABLE = " DROP TABLE IF EXISTS "
            + TABLE_NAME;

    public static final String CREATE_TABLE = "create table  if not exists "
            + TABLE_NAME + " ("
            + PHONE_NUM + " TEXT, " + DATE + " TEXT, " + COMPANY_NUM + " TEXT, "
            + FUNCTION_MODE + " TEXT, " + CLICK_TIMES
            + " INTEGER DEFAULT '0' "
            + " ) ";
       
    
    //根据日期查询相关项
    public static JSONArray queryDataByDate(
            String date) {
        JSONArray menuActionArr = null;
        XyCursor cur = null;
        try {
        	
        	 cur = DBManager.query(TABLE_NAME, new String[] { PHONE_NUM, COMPANY_NUM, DATE,
                     FUNCTION_MODE, CLICK_TIMES }, DATE + " < ? ",
                     new String[] { date }, null, null, null, null);
        	
        	 menuActionArr = BaseManager.loadArrDataFromCursor(new String[] { PHONE_NUM, COMPANY_NUM, DATE,
                    FUNCTION_MODE, CLICK_TIMES }, cur);
        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager queryDataByDate",e);
            /*SDK-536 hewengao/20160713 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return menuActionArr;
    }
    
    

    public static List<menuActionInfo> queryDates(String updateDate) {
        XyCursor cur = null;
        List<menuActionInfo> list = new ArrayList<menuActionInfo>();
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { DATE }, DATE
                    + " < ? ", new String[] { updateDate }, DATE, null, null,
                    null);
            if (cur != null && cur.getCount() > 0) {
                int dateIndex = cur.getColumnIndex(DATE);

                menuActionInfo info = null;
                while (cur.moveToNext()) {
                    info = new menuActionInfo();
                    info.date = cur.getString(dateIndex);
                    list.add(info);
                }
                return list;
            }
        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager queryDates",e);
            /*SDK-536 hewengao/20160713 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return list;
    }

    public static List<menuActionInfo> queryByDate(String date) {
        List<menuActionInfo> infoList = new ArrayList<menuActionInfo>();
        XyCursor cur = null;
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { PHONE_NUM, COMPANY_NUM, DATE,
                    FUNCTION_MODE, CLICK_TIMES }, DATE + " = ? ",
                    new String[] { date }, null, null, null, null);
            if (cur != null && cur.getCount() > 0) {
                int phoneNumIndex = cur.getColumnIndex(PHONE_NUM);
                int companyNumIndex = cur.getColumnIndex(COMPANY_NUM);
                int dateIndex = cur.getColumnIndex(DATE);
                int functionModeIndex = cur.getColumnIndex(FUNCTION_MODE);
                int clickTimesIndex = cur.getColumnIndex(CLICK_TIMES);
                menuActionInfo info = null;
                while (cur.moveToNext()) {
                    info = new menuActionInfo();
                    info = new menuActionInfo();
                    info.phoneNum = cur.getString(phoneNumIndex);
                    info.companyNum = cur.getString(companyNumIndex);
                    info.date = cur.getString(dateIndex);
                    info.functionMode = cur.getString(functionModeIndex);
                    info.clickTimes = cur.getInt(clickTimesIndex);
                    infoList.add(info);
                }

            }
        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager queryByDate",e);
            /*SDK-536 hewengao/20160713 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return infoList;
    }

    public static menuActionInfo queryInfoByParameter(String companyNum, String functionMode) {
        XyCursor cur = null;
        menuActionInfo info = null;
        String date = DateUtils.getCurrentTimeString("yyyyMMdd");
        try {
            cur = DBManager.query(TABLE_NAME, new String[] { PHONE_NUM, COMPANY_NUM, DATE,
                    FUNCTION_MODE, CLICK_TIMES }, COMPANY_NUM + " = ? and " + DATE
                    + " = ? and " + FUNCTION_MODE + " = ? ", new String[] { companyNum, date, functionMode }, null, null,
                    null, "1");
            if (cur != null && cur.getCount() > 0) {
                int phoneNumIndex = cur.getColumnIndex(PHONE_NUM);
                int companyNumIndex = cur.getColumnIndex(COMPANY_NUM);
                int dateIndex = cur.getColumnIndex(DATE);
                int functionModeIndex = cur.getColumnIndex(FUNCTION_MODE);
                int clickTimesIndex = cur.getColumnIndex(CLICK_TIMES);

                while (cur.moveToNext()) {
                    info = new menuActionInfo();
                    info.phoneNum = cur.getString(phoneNumIndex);
                    info.companyNum = cur.getString(companyNumIndex);
                    info.date = cur.getString(dateIndex);
                    info.functionMode = cur.getString(functionModeIndex);
                    info.clickTimes = cur.getInt(clickTimesIndex);
                }
                return info;
            }else{
				info = null;
			}
        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager queryInfoByParameter",e);
            /*SDK-536 hewengao/20160713 end*/
        } finally {
            XyCursor.closeCursor(cur, true);
        }
        return info;
    }

    public static long insertOrUpdateParse(HashMap<String, String> paramsMap) {
        String phoneNum = paramsMap.get("phoneNum");
        String companyNum = paramsMap.get("companyNum");
        String functionMode = paramsMap.get("functionMode");
        long result = -1;
        menuActionInfo info = null;
        ContentValues values = null;
        try {
            if (!StringUtils.isNull(companyNum) && !StringUtils.isNull(functionMode) ) {
                info = queryInfoByParameter(companyNum, functionMode);
            }

            if (info != null) {

                info.clickTimes = info.clickTimes + 1;
                values = setContentValues(info);
                DBManager.update(TABLE_NAME, values, COMPANY_NUM + " = ? and " + FUNCTION_MODE + " = ? and "
                        + DATE + " = ? ", new String[] { info.companyNum,
                        info.functionMode, info.date });
                return 0;
            } else {
                info = new menuActionInfo();
                info.phoneNum = Signaturer.sha256Encode(phoneNum);
                info.companyNum = companyNum;
                info.functionMode = functionMode;

                String date = DateUtils.getCurrentTimeString("yyyyMMdd");
                info.date = date;
                info.clickTimes = 1;
                // 插入一条记录
                values = setContentValues(info);
                result = DBManager.insert(TABLE_NAME, values);
            }
        } catch (Throwable e) {
            /*SDK-550 hewengao 20160719 start*/
        	 LogManager.w(Constant.TAG, "getSimIndex: ", e);
        	 /*SDK-550 hewengao 20160719 end*/
        } finally {
        }
        return result;
    }


    public static ContentValues setContentValues(menuActionInfo info) {
        ContentValues values = new ContentValues();
        values.put(PHONE_NUM, info.phoneNum);
        values.put(COMPANY_NUM, info.companyNum);
        values.put(DATE, info.date);
        values.put(FUNCTION_MODE, info.functionMode);
        values.put(CLICK_TIMES, info.clickTimes);
        return values;
    }

    public static void deleteAll() {
        try {
            DBManager.delete(TABLE_NAME, null, null);

        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager deleteAll",e);
            /*SDK-536 hewengao/20160713 end*/
        }
    }

    public static void deleteInfoByDate(String date) {
        try {
            DBManager.delete(TABLE_NAME, DATE + " < ?", new String[] { date });
        } catch (Throwable e) {
            /*SDK-536 hewengao/20160713 start*/
            LogManager.w(Constant.TAG, "MenuActionManager deleteInfoByDate",e);
            /*SDK-536 hewengao/20160713 end*/
        }
    }

}
